using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace PackTool
{
    public class TextLoad
    {
#if USER_ALLRESOURCES
        static Dictionary<string, string> TextAsset = new Dictionary<string, string>();
        public static void Init()
        {
#if UNITY_STANDALONE_WIN
            TextAsset.Clear();
            string path = Application.dataPath + "/Data/Config";
			if (!System.IO.Directory.Exists(path))
				return;

            int startindex = Application.dataPath.Length;
            string[] files = System.IO.Directory.GetFiles(path, "*", System.IO.SearchOption.AllDirectories);
            foreach (string file in files)
            {
                string s = file.ToLower();
                if (s.EndsWith(".csv") || s.EndsWith(".xml") || s.EndsWith(".txt"))
                {
                    System.IO.StreamReader Reader = new System.IO.StreamReader(file, System.Text.Encoding.UTF8);
                    if (Reader != null)
                    {
                        s = file.Substring(startindex+1);
                        s = s.Replace('\\', '/');
                        TextAsset.Add(s.ToLower(), Reader.ReadToEnd());
                        //Debug.Log(s);
                    }
                }
            }
#endif
        }

        public static string GetText(string key)
        {
#if UNITY_STANDALONE_WIN
            string data = null;
            if (TextAsset.TryGetValue(key.ToLower(), out data))
                return data;

            return null;
#else
            TextAsset asset = AllResources.Instance.GetTextAsset(key);
            if (asset == null)
            {   
                return null;
            }
            return asset.text;
#endif
        }

#elif USE_RESOURCESEXPORT
        static Dictionary<string, string> TextAsset = new Dictionary<string, string>();
        static public bool IsEnd { get; private set; }
        public static void Init()
        {
            IsEnd = false;

            List<string> AllFiles = new List<string>();
            foreach (string file in AllFileList.FileList)
            {
                if (file.StartsWith("Data/Config"))
                    AllFiles.Add(file);
            }

            if (AllFiles.Count == 0)
            {
                IsEnd = true;
            }
            else
            {
                ResourceDown.Instance.Load(ResourcesPath.FinalUrl(AllFiles[0]), LoadFileEnd, new object[2] { AllFiles, 0 });
            }
        }

        static void LoadFileEnd(WWW www, object p)
        {
            object[] pp = (object[])p;
            List<string> files = (List<string>)pp[0];
            int index = (int)pp[1];
            index++;
            if (index < files.Count)
            {
                ResourceDown.Instance.Load(ResourcesPath.FinalUrl(files[index]), LoadFileEnd, new object[2] { files, index });
            }
            else
            {
                IsEnd = true;
            }

            byte[] bytes = www.bytes;
            if (bytes.Length > 3 && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF)
            {
                TextAsset.Add(files[index - 1].ToLower(), System.Text.Encoding.UTF8.GetString(www.bytes, 3, www.bytes.Length - 3));
            }
            else
            {
                TextAsset.Add(files[index - 1].ToLower(), www.text);
            }
            Debug.Log(string.Format("Config:{0}", www.url));
        }

        public static string GetText(string key)
        {
            string data = null;
            if (TextAsset.TryGetValue(key.ToLower(), out data))
                return data;

            return null;
        }
#else
        public static string GetText(string key)
        {
            TextAsset asset = (TextAsset)Resources.LoadAssetAtPath("Assets/" + key, typeof(TextAsset));
            if (asset == null)
                return null;

            return asset.text;
        }

        public static void Init()
        {

        }
#endif
    }
}